//
//  BuyInfoTableViewController.m
//  EasyBuy
//
//  Created by apple on 12-10-26.
//  Copyright (c) 2012年 shen donghao. All rights reserved.
//

#import "BuyInfoTableViewController.h"
#import "BuyInfoTableViewCell.h"
#import "EasyBuy.h"
#import "EasyBuyService.h"
#import "BuyInfoData.h"
#import "UserBuiesTableViewController.h"
#import "BuyInfoScrollViewTableViewCell.h"
#import "BuyInfoTitleTableViewCell.h"
#import "BuyInfosTableViewCell.h"
#import "GoogleMapWithBuyViewController.h"

@interface BuyInfoTableViewController ()

@end

@implementation BuyInfoTableViewController
@synthesize objectId;
@synthesize buyInfoDataArray;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title = @"宝贝详情";
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    NSLog(@"objectId = %d",objectId);
    //self.tableView.style = UITableViewStylePlain;
    tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480-20-44-44) style:UITableViewStylePlain];
    tableview.delegate = self;
    tableview.dataSource = self;
    [tableview setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.view addSubview:tableview];
    
    UIBarButtonItem *LikeItBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"喜欢" style:UIBarButtonItemStyleDone target:self action:nil];
    self.navigationItem.rightBarButtonItem = LikeItBarButtonItem;
    [LikeItBarButtonItem release];
    
    UITabBar *tabBar = [[UITabBar alloc] initWithFrame:CGRectMake(0, 480-480-44-20-53, 320, 53)];
    tabBar.backgroundImage = [UIImage imageNamed:@"buyInfo-btn-bottom"];
    tabBar.backgroundColor = [UIColor redColor];
    [self.view addSubview:tabBar];
    [tabBar release];
    [self initObjectDetailData];

}

-(void)initObjectDetailData{
    
    EasyBuyService *service =  [[EasyBuyService alloc] init];
    EasyBuyRequest<RequestProtocol> *request = [[EasyBuyRequest alloc] initObjectDetailRequestWithObjectId:5];
    [service executeFocusRequest:request :RECEIVER_OBJECT_DETAIL];
    [[NSNotificationCenter defaultCenter] addObserver:self
											 selector:@selector(onReceivedObjectDetailData:)
												 name:RECEIVER_OBJECT_DETAIL
											   object:nil];
    [service release];
    [request release];
    
}

-(void)onReceivedObjectDetailData:(NSNotification *)notification{
    NSLog(@"%@",notification.name);
    NSArray *objectDetailArray = notification.object;
    buyInfoDataArray = objectDetailArray;
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

-(void)onReceivedControllerMsgUser:(NSNotification *)notification{
    
    //    NSNumber *contentIdNumber = notification.object;
    UserBuiesTableViewController *userBuiesTableViewController = [[UserBuiesTableViewController alloc] init];
    //    userBuiesTableViewController.objectId = [contentIdNumber intValue];
    [self.navigationController pushViewController:userBuiesTableViewController animated:YES];
    [userBuiesTableViewController release];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    // Return the number of rows in the section.
    return 4;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return 42;
    }
    if (indexPath.row == 1) {
        return 200;
    }
    if (indexPath.row == 2) {
        return 40;
    }
    if (indexPath.row == 3) {
        return 180;
    }
    return 44;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //    BuyInfoData *buyInfoData = [self.buyInfoDataArray objectAtIndex:indexPath.row];
    
    
    
    static NSString *CellIdentifier = @"Cell";
    static NSString *scrollCellIdentifier = @"scrollCell";
    static NSString *titleCellIdentifier = @"titleCell";
    static NSString *infoCellIdentifier = @"infoCell";
    
    if (indexPath.row == 0) {
        BuyInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(0, 0, 320, 40);
            btn.backgroundColor = [UIColor clearColor];
            [btn addTarget:self action:@selector(onReceivedControllerMsgUser:) forControlEvents:UIControlEventTouchUpInside];
            [self.view addSubview:btn];
            
            cell = [[[BuyInfoTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]autorelease];
            cell.accessoryType = UITableViewCellAccessoryNone; 
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell initCellWithBuyInfoData:nil];
        return cell;
    }
    if (indexPath.row == 1) {
        BuyInfoScrollViewTableViewCell *scrollCell = [tableView dequeueReusableCellWithIdentifier:scrollCellIdentifier];
        if (scrollCell == nil) {
            scrollCell = [[[BuyInfoScrollViewTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:scrollCellIdentifier]autorelease];
            scrollCell.accessoryType = UITableViewCellAccessoryNone; 
        }
        scrollCell.selectionStyle = UITableViewCellSelectionStyleNone;
        [scrollCell initCellWithBuyScrollInfoData:nil];
        return scrollCell;
    }
    if (indexPath.row == 2) {
        BuyInfoTitleTableViewCell *titleCell = [tableView dequeueReusableCellWithIdentifier:titleCellIdentifier];
        if (titleCell == nil) {
            titleCell = [[[BuyInfoTitleTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:titleCellIdentifier]autorelease];
            titleCell.accessoryType = UITableViewCellAccessoryNone; 
            titleCell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        
        [titleCell initCellWithBuyTitleInfoData:nil];
        return titleCell;
    }
    if (indexPath.row == 3) {
        BuyInfosTableViewCell *infoCell = [tableView dequeueReusableCellWithIdentifier:infoCellIdentifier];
        if (infoCell == nil) {
            infoCell = [[[BuyInfosTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:infoCellIdentifier]autorelease];
            infoCell.accessoryType = UITableViewCellAccessoryNone;
            infoCell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        [infoCell initCellWithBuyInfosData:nil];
        return infoCell;
    }
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, tableview.frame.size.height, 320, 44)];    
    
    UIButton *weiboParticipateButton = [UIButton buttonWithType:UIButtonTypeCustom];
    weiboParticipateButton.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"weibo_share"]];
    [weiboParticipateButton setTitle:nil forState:UIControlStateNormal];
    [weiboParticipateButton addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
    weiboParticipateButton.frame = CGRectMake(10,10, 64, 27);
    [footerView addSubview:weiboParticipateButton];
    //[self.tableView.tableFooterView addSubview:weiboParticipateButton];
    
    UIButton *locationButton = [UIButton buttonWithType:UIButtonTypeCustom];
    locationButton.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"buyLocation"]];
    [locationButton setTitle:nil forState:UIControlStateNormal];
    [locationButton addTarget:self action:@selector(setBuyLocaition) forControlEvents:UIControlEventTouchUpInside];
    locationButton.frame = CGRectMake(320 - 96, 10, 86, 25);
    [footerView addSubview:locationButton];
    
    if (indexPath.row == 3){
        tableview.tableFooterView = footerView;
    }
    [footerView release];
}

- (void)setBuyLocaition{
    GoogleMapWithBuyViewController *googleMapWithBuyViewController = [[GoogleMapWithBuyViewController alloc] init];
    BuyInfoData *buyInfoData = [[BuyInfoData alloc] init];
    [self.navigationController pushViewController:googleMapWithBuyViewController animated:YES];
    //    [googleMapWithBuyViewController setBuyLocationInGoogleMapWithlongitude:*(buyInfoData.longitude) latitude:*(buyInfoData.latitude)];
    double longitude = 116.10000000;
    double latitude = 40.10000000;
    NSString *title = @"正品！卡西欧EF男表转让";
    NSString *subtitle = @"北京 朝阳区";
    [googleMapWithBuyViewController setBuyLocationInGoogleMapWithlongitude:longitude latitude:latitude title:title subtitle:subtitle];
    
    [buyInfoData release];
    [googleMapWithBuyViewController release];
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
